/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import HandlerClass.AddressHandler;
import HandlerClass.CargoHandler;
import HandlerClass.CertificateHandler;
import HandlerClass.CustomerHandler;
import HandlerClass.DistanceHandler;
import HandlerClass.DriverHandler;
import HandlerClass.TaskHandler;
import HandlerClass.VehicleHandler;
import ObjectClass.SQLCommunication;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.JOptionPane;

/**
 *
 * @author Maja Gliese
 */
public class Main extends javax.swing.JFrame {

    private int curCustomerPhone;
    private StartPanel startPanel;
    private SøgOpgaver søgOpgaver;
    private OpretOpgave opretOpgave;
    private OpretKunde opretKunde;
    private CardLayout layout;
    private final SQLCommunication sql;
    private AddressHandler addressHandler;
    private CustomerHandler customerHandler;
    private CertificateHandler certificateHandler;
    private DistanceHandler distanceHandler;
    private DriverHandler driverHandler;
    private VehicleHandler vehicleHandler;
    private TaskHandler taskHandler;
    private CargoHandler cargoHandler;

    public Main() {
        initComponents();
        sql = new SQLCommunication(this);

        setIcon();
        instantieringAfHandlers();
        lavPanelLayout();
        visStartPanel();
//        visOpretOpgave();
        try {
            sql.openConn();
        } catch (SQLException ex) {
//            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);  
//            JOptionPane.showMessageDialog(this, "Forbindelsen til databasen mislykkedes desværre. Tjek indstillinger eller spørg udvikleren.","Fejlmeddelelse",3);
            System.out.println("Der blev ikke oprettet forbindelse til databasen");
            JOptionPane.showOptionDialog(this, "Forbindelsen til databasen mislykkedes desværre.\nTjek indstillinger eller spørg udvikleren.", "Fejlmeddelelse", 2, 3, null, null, null);
        }
    }

    public void warningMsg(String fejl) {
        JOptionPane.showMessageDialog(this, fejl, "Advarsel!", 2);
    }

    public void visStartPanel() {
        layout.show(MainPanel, "StartPanel");
    }

    public void visSøgOpgaver() {
        layout.show(MainPanel, "SøgOpgaver");
    }

    public void visOpretOpgave() {
        layout.show(MainPanel, "OpretOpgave");
    }

    public void visOpretKundeDialog() {
        opretKunde.setVisible(true);
    }
// <editor-fold defaultstate="collapsed" desc="GetHandlers"> 

    public SQLCommunication getSql() {
        return sql;
    }

    public AddressHandler getAddressHandler() {
        return addressHandler;
    }

    public CustomerHandler getCustomerHandler() {
        return customerHandler;
    }

    public CertificateHandler getCertificateHandler() {
        return certificateHandler;
    }

    public DistanceHandler getDistanceHandler() {
        return distanceHandler;
    }

    public DriverHandler getDriverHandler() {
        return driverHandler;
    }

    public VehicleHandler getVehicleHandler() {
        return vehicleHandler;
    }

    public TaskHandler getTaskHandler() {
        return taskHandler;
    }

    public CargoHandler getCargoHandler() {
        return cargoHandler;
    }

    public int getCurCustomerPhone() {
        return curCustomerPhone;
    }

    public void setCurCustomerPhone(int curCustomerPhone) {
        this.curCustomerPhone = curCustomerPhone;
    }

    // </editor-fold>
    public void instantieringAfHandlers() {
        addressHandler = new AddressHandler(this);
        customerHandler = new CustomerHandler(this);
        vehicleHandler = new VehicleHandler(this);
        certificateHandler = new CertificateHandler(this);
        distanceHandler = new DistanceHandler(this);
        driverHandler = new DriverHandler(this);
        cargoHandler = new CargoHandler(this);
        taskHandler = new TaskHandler(this);
    }

    private void lavPanelLayout() {
        layout = (CardLayout) MainPanel.getLayout();

        startPanel = new StartPanel(this);
        søgOpgaver = new SøgOpgaver(this);
        opretOpgave = new OpretOpgave(this);
        opretKunde = new OpretKunde(this, true);
        MainPanel.add(startPanel);
        MainPanel.add(søgOpgaver);
        MainPanel.add(opretOpgave);
        layout.addLayoutComponent(startPanel, "StartPanel");
        layout.addLayoutComponent(søgOpgaver, "SøgOpgaver");
        layout.addLayoutComponent(opretOpgave, "OpretOpgave");
    }

    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Lastbil.png")));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MainPanel = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        MenuNyOpgave = new javax.swing.JMenuItem();
        MenuNyKunde = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        MenuSoegOpgaver = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        MenuCloseKnap = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Næstved Vejtransport");
        setMinimumSize(new java.awt.Dimension(800, 600));

        MainPanel.setLayout(new java.awt.CardLayout());
        getContentPane().add(MainPanel, java.awt.BorderLayout.CENTER);

        jMenuBar1.setBackground(new java.awt.Color(51, 51, 51));
        jMenuBar1.setForeground(new java.awt.Color(255, 255, 255));

        jMenu1.setText("Filer");

        MenuNyOpgave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        MenuNyOpgave.setText("Ny opgave");
        MenuNyOpgave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuNyOpgaveActionPerformed(evt);
            }
        });
        jMenu1.add(MenuNyOpgave);

        MenuNyKunde.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_K, java.awt.event.InputEvent.CTRL_MASK));
        MenuNyKunde.setText("Ny Kunde");
        MenuNyKunde.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuNyKundeActionPerformed(evt);
            }
        });
        jMenu1.add(MenuNyKunde);
        jMenu1.add(jSeparator1);

        MenuSoegOpgaver.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        MenuSoegOpgaver.setText("Søg Opgaver");
        MenuSoegOpgaver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuSoegOpgaverActionPerformed(evt);
            }
        });
        jMenu1.add(MenuSoegOpgaver);
        jMenu1.add(jSeparator2);

        MenuCloseKnap.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        MenuCloseKnap.setText("Afslut");
        MenuCloseKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuCloseKnapActionPerformed(evt);
            }
        });
        jMenu1.add(MenuCloseKnap);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Værktøjer");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Indstillinger");
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void MenuNyOpgaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuNyOpgaveActionPerformed
        visOpretOpgave();
    }//GEN-LAST:event_MenuNyOpgaveActionPerformed

    private void MenuNyKundeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuNyKundeActionPerformed
        visOpretKundeDialog();
    }//GEN-LAST:event_MenuNyKundeActionPerformed

    private void MenuSoegOpgaverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuSoegOpgaverActionPerformed
        visSøgOpgaver();
    }//GEN-LAST:event_MenuSoegOpgaverActionPerformed

    private void MenuCloseKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuCloseKnapActionPerformed
        sql.closeConn();
        System.out.println("Forbindelsen til databasen er lukket,");
        System.exit(1);
    }//GEN-LAST:event_MenuCloseKnapActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel MainPanel;
    private javax.swing.JMenuItem MenuCloseKnap;
    private javax.swing.JMenuItem MenuNyKunde;
    private javax.swing.JMenuItem MenuNyOpgave;
    private javax.swing.JMenuItem MenuSoegOpgaver;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    // End of variables declaration//GEN-END:variables
}
